package com.itheima.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description:
 * @author: Kris Wu
 * @time: 2020/11/19 22:31
 */
@Transactional
@Service(interfaceClass = PermissionService.class)
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;

    /**
     * 分页查询权限列表
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        //第一步：设置分页参数
        PageHelper.startPage(currentPage,pageSize);
        //第二步：查询数据库
        Page<Permission> permissionPage = permissionDao.selectByCondition(queryString);
        return new PageResult(permissionPage.getTotal(),permissionPage.getResult());
    }
    /**
     * 新增权限
     * @param permission
     */
    @Override
    public void add(Permission permission) {
         permissionDao.add(permission);
    }
    /**
     * 根据id查询权限数据
     * @param id
     * @return
     */
    @Override
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }

    /**
     * 根据id更新权限数据
     * @param permission
     */
    @Override
    public void edit(Permission permission) {
        permissionDao.edit(permission);
    }
    /**
     *根据id删除权限数据
     * @param permissionId
     * @return
     */
    @Override
    public void deleteById(Integer permissionId) {
        //根据权限id查询与角色相关联的数量
        int count = permissionDao.findCountByPermissionId(permissionId);
        if (count>0) {
            throw new RuntimeException(MessageConstant.DELETE_PERMISSION_FALL2);
        }
        permissionDao.deleteById(permissionId);
    }
    /**
     * 查询所有权限列表
     * @return
     */
    @Override
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }
    /**
     * 校验权限名称是否重复
     * @return
     */
    @Override
    public Permission findPermissionByName(String name) {
        return permissionDao.findPermissionByName(name);
    }
    /**
     * 校验权限关键字是否重复
     * @return
     */
    @Override
    public Permission findPermissionByKeyword(String keyword) {
        return permissionDao.findPermissionByKeyword(keyword);
    }
    /**
     * 查询与角色相关联数据
     * @return
     */
    @Override
    public List<Role> queryRoleById(Integer id) {
        return permissionDao.queryRoleById(id);
    }

}
